import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2023-01-28
 * Time: 9:02
 */
public class Test {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char a = s.charAt(i);
            if(a == '(' || a == '{' || a == '[') {
                stack.push(a);
            }else {
                if(stack.isEmpty()) {
                    return false;
                }else {
                    if(!stack.pop().equals(a)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static int evalRPN1(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for(int i = 0; i < tokens.length; i++) {
            String a = tokens[i];
            if(a.equals("+") || a.equals("-") || a.equals("*") || a.equals("/")) {
                int b = stack.pop();
                int c = stack.pop();
                switch (a) {
                    case "+":
                        stack.push(b+c);
                        break;
                    case "-":
                        stack.push(b-c);
                        break;
                    case "*":
                        stack.push(b*c);
                        break;
                    case "/":
                        stack.push(b/c);
                        break;
                }
            }else {
                stack.push(Integer.parseInt(a));
            }
        }
        return stack.pop();
    }

        public static int evalRPN(String[] tokens) {
            Deque<Integer> stack = new LinkedList<Integer>();
            int n = tokens.length;
            for (int i = 0; i < n; i++) {
                String token = tokens[i];
                if (isNumber(token)) {
                    stack.push(Integer.parseInt(token));
                } else {
                    int num2 = stack.pop();
                    int num1 = stack.pop();
                    switch (token) {
                        case "+":
                            stack.push(num1 + num2);
                            break;
                        case "-":
                            stack.push(num1 - num2);
                            break;
                        case "*":
                            stack.push(num1 * num2);
                            break;
                        case "/":
                            stack.push(num1 / num2);
                            break;
                        default:
                    }
                }
            }
            return stack.pop();
        }

        public static boolean isNumber(String token) {
            return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
        }

    public static void main(String[] args) {
        String[] tokens = new String[]{"4","13","5","/","+"};
        int a = evalRPN(tokens);
        System.out.println(a);
    }
}
